#include "wxsboolproperty.h"

#include <prep.h>

#define VALUE   wxsVARIABLE(Object,Offset,bool)

wxsBoolProperty::wxsBoolProperty( const wxString& PGName, const wxString& DataName, long _Offset, bool _Default, int Priority ):
  wxsProperty( PGName, DataName, Priority ),
  Offset( _Offset ),
  Default( _Default ) {}


void wxsBoolProperty::PGCreate( wxsPropertyContainer* Object, wxPropertyGridManager* Grid, wxPGId Parent ) {
  wxPGId ID = Grid->AppendIn( Parent, new wxBoolProperty( GetPGName(), wxPG_LABEL, VALUE ) );
  Grid->SetPropertyAttribute( ID, wxPG_BOOL_USE_CHECKBOX, 1L, wxPG_RECURSE );
  PGRegister( Object, Grid, ID );
}

bool wxsBoolProperty::PGRead( M_Code_unused wxsPropertyContainer* Object,
                              wxPropertyGridManager* Grid, wxPGId Id,
                              M_Code_unused long Index ) {
  VALUE = Grid->GetPropertyValue( Id ).GetBool();
  return true;
}

bool wxsBoolProperty::PGWrite( M_Code_unused wxsPropertyContainer* Object,
                               wxPropertyGridManager* Grid, wxPGId Id,
                               M_Code_unused long Index ) {
  Grid->SetPropertyValue( Id, VALUE );
  return true;
}

bool wxsBoolProperty::XmlRead( M_Code_unused wxsPropertyContainer* Object,
                               TiXmlElement* Element ) {
  if( !Element ) {
    VALUE = Default;
    return false;
  }
  const char* Text = Element->GetText();
  if( !Text ) {
    VALUE = Default;
    return false;
  }
  VALUE = atol( Text ) != 0;
  return true;
}

bool wxsBoolProperty::XmlWrite( M_Code_unused wxsPropertyContainer* Object,
                                TiXmlElement* Element ) {
  if( VALUE != Default ) {
    Element->InsertEndChild( TiXmlText( VALUE ? "1" : "0" ) );
    return true;
  }
  return false;
}

bool wxsBoolProperty::PropStreamRead( M_Code_unused wxsPropertyContainer* Object,
                                      wxsPropertyStream* Stream ) {
  return Stream->GetBool( GetDataName(), VALUE, Default );
}

bool wxsBoolProperty::PropStreamWrite( M_Code_unused wxsPropertyContainer* Object,
                                       wxsPropertyStream* Stream ) {
  return Stream->PutBool( GetDataName(), VALUE, Default );
}
